package admin.forms;

import helpers.StringHelper;

import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import admin.models.CategoryModel;

import entities.Category;


public class CategoryForm {
	public static TreeMap<String, Object> checkValid(HttpServletRequest request, Category category) {
		TreeMap<String, Object> form = new TreeMap<String, Object>();
		TreeMap<String, String> check = new TreeMap<String, String>();
		
		String id = (String) request.getParameter("id");
		String name = (String) request.getParameter("name");
		String desc = (String) request.getParameter("desc");
		String parentId = (String) request.getParameter("parentId");
		String status = (String) request.getParameter("status");
		
		if (id != null && !id.isEmpty()) {
			category.setId(id);
		}
		
		if (name == null || name.isEmpty()) {
			check.put("name", "Name is not null.");
			category.setName("");
		} else {
			category.setName(name);
		}
		
		if (desc == null || desc.equals("")) {
			check.put("desc", "Description is not empty");
			category.setDesc(desc);
		} else {
			category.setDesc(desc);
		}
		
		if (parentId != null) {
			category.setParentId(parentId);
		}
		
		if (status != null && !status.equalsIgnoreCase("")) {
			category.setStatus(Integer.parseInt(status));
		}
		
		if (name != null && !name.isEmpty()) {
			String alias = StringHelper.replace(name);
			if (!alias.isEmpty()) {
				category.setAlias(alias);
			}
		}
		
		form.put("checkValid", check);
		form.put("category", category);
		
		return form;
	}
}
